package base;

import java.util.Scanner;

/*    螺旋数阵
 * 
 */
public class SpiralArray {

	public static void main(String[] args) {
	
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入螺旋数列阶数");
		int num = sc.nextInt();
		SpiralArray(num);
		
	}

	private static void SpiralArray(int n) {
		int arr[][] = new int[n][n];
		for(int i=0;i<arr.length;i++){
			for(int j=0;j<arr[i].length;j++){
				arr[i][j]=0;
			}
		}
		
		
		int min = 0, max = n - 1;
		int i = 0, j = 0;
		for (int x = 0; x <n*n; x++) {
			arr[i][j] = x + 1;
			if (i == min && j < max) {
				j++;
			} else if (j == max && i < max) {
				i++;
			} else if (i == max && j > min) {
				j--;
			} else if (i > min && j == min) {
				i--;
			}
			if (i - 1 == min && j == min) {
				min++;
				max--;
			}
		}

		for (i = 0; i < arr.length; i++) {
			for (j = 0; j < arr[i].length; j++) {
				System.out.print(arr[i][j] + "\t");
			}
			System.out.println();
		}

	}

}
